package org.xy.manager;

import java.util.Map;

import org.apache.log4j.Logger;
import org.xy.bean.Bean;
import org.xy.bean.BeanWapper;
import org.xy.parser.RootParser;

public class ManualDenpencyManager implements IBeanAccessor<Class<?>> {
	private static final Logger logger = Logger
			.getLogger(ManualDenpencyManager.class.getName());

	private static Map<String, BeanWapper> beanWappers = null;
	private static Object lock = new Object();

	public BeanWapper getBeanWapper(String name, Class<?> clazz) {
		BeanWapper beanWapper = beanWappers.get(name);
		String className = beanWapper.getCurrent().getClassName();

		if (className.equals(clazz.getName())) {
			return beanWapper;
		}

		return null;
	}

	public ManualDenpencyManager() {
		synchronized (lock) {
			if (beanWappers != null) {
				beanWappers.clear();
			}

			Map<String, Bean> beans = BeanDefinitionReader
					.getBeans(BeanDefinitionReader.TYPE_XML);

			beanWappers = new RootParser(beans).getBeanWappers();

			logger.info("Size of BeanWappers is " + beanWappers.size());
		}
	}
}
